'use strict';
const HTM = require('./utils').html;
const checkDate = require('./utils').checkDate;
const db = require("../coSqlite3");

exports.AddBook = function*(req) {
    let res = undefined;
    const body = req.body;
    const base_err = ["sql语句执行错误", "该书已存在", "提交参数有误: "];

    //let dateFormat =dateFormat =/^(\d{4})-(\d{2})-(\d{2})$/;

    // 格式检查
    if (body.bID == '' || body.bName == '' || body.bCnt == '') {
        res = base_err[2] + "未填写必填参数!";
    } else {
        // TODO 检查每个输入的长度、类型是否符合要求
        if(body.bID.length<31 && body.bName.length<31 && Math.round(body.bCnt)==body.bCnt){
            //if(body.bPub.length > 30 || body.bAuthor.length > 20 || body.bMem.length > 30 || dateFormat.test(body.rDate)!=true ) {
            if(body.bPub.length > 30 || body.bAuthor.length > 20 || body.bMem.length > 30 || !checkDate(body.bDate) ) {
                res = base_err[2] + "填写格式有误！";
            }
        }
        else{
            res = base_err[2] + "必要参数填写格式有误！";
        }

    }
    if (res) return HTM.begin + "<div id='result' style='display:none'>2</div>" + res + HTM.end;

    // 检查ID为bID的书是否存在
    let query_sql = "select * from book where bookID = '" + body.bID + "'";
    try {
        let result = yield db.execSQL(query_sql);
        if (result.length !== 0) {
            res = base_err[1];
        } 
    } catch(err) {
        res = base_err[0] + err;
    }
    if (res) return HTM.begin + "<div id='result' style='display:none'>1</div>" + res + HTM.end;

    // 插入新书
    let insert_sql = "insert into book(bookID,bookName,bookPub,bookDate,bookAuthor,bookMem,bookCnt) values(?,?,?,?,?,?,?)";
    try {
        let params = [];
        Object.keys(body).forEach((key, index) => {
            if (body[key] !== undefined) {
                params.push(body[key]);
            } else {
                params.push(null);
            }
        })
        yield db.execSQL(insert_sql, params);
        res = "添加书籍成功！";
    } catch (err) {
        res = base_err[0] + err;
    }

    return HTM.begin + "<div id='result' style='display:none'>0</div>" + res + HTM.end;
};


exports.AddBookCnt = function*(req) {
    // TODO 添加书籍数量

    let body = req.body;

    if(body.bID){
        let bkCHK = yield  db.execSQL("select * from book where bookID = ?",[body.bID]);

        if(bkCHK[0]){
            //格式正确
            if(body.bID.length<31 && Math.round(body.bCnt) == body.bCnt){


                //update 数量 并返回0
                yield db.execSQL("")


            }
            //不正确
            else{
                return HTM.begin + "<div id='result' style='display:none'>2</div>" + "未填写必填参数" + HTM.end;
            }
        }
        //为空
        else{
            return HTM.begin + "<div id='result' style='display:none'>1</div>" + "该书不存在" + HTM.end;
        }
    }
   //未填写书号
   else{
        return HTM.begin + "<div id='result' style='display:none'>2</div>" + "未填写必填参数" + HTM.end;
      }

}


exports.DelBookCnt = function*(req) {
    // TODO 减少/删除书籍数量


}

exports.ChangeBookInfo = function*(req) {
    // TODO 修改书籍信息
}

exports.QueryBook = function*(req) {
    // TODO 查询书籍
}